﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using QLNhaSach;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace DataAccessLayer
{
    public class TheLoaiDAL
    {
        DataService ds = new DataService();
        public TheLoaiDAL()
        {
        }
        public TheLoai GetTheLoaiByID(int id)
        {
            TheLoai tl = null;
            try
            {
                string sql = "SELECT * FROM THELOAI WHERE TL_MATHELOAI=" + id;
                SqlDataReader rd = ds.ExcuteDataReader(sql);
                while (rd.Read())
                {
                    tl = new TheLoai();
                    tl.MaTheLoai = (int)rd["TL_MaTheLoai"];
                    tl.TenTheLoai = rd["TL_TenTheLoai"].ToString();
                }
            }
            catch (Exception)
            {
                throw;
            }
            return tl;

        }
        public TheLoai GetIDByName(string name)
        {
            TheLoai tl = null;
            try
            {
                string sql = "SELECT * FROM THELOAI WHERE TL_TENTHELOAI=N'" + name +"'";
                SqlDataReader rd = ds.ExcuteDataReader(sql);
                while (rd.Read())
                {
                    tl = new TheLoai();
                    tl.MaTheLoai = (int)rd["TL_MaTheLoai"];
                    tl.TenTheLoai = rd["TL_TenTheLoai"].ToString();
                }
            }
            catch (Exception)
            {
                throw;
            }
            return tl;

        }
        public DataTable GetDanhSachTheLoai()
        {
            DataTable tl = null;
            try
            {
                string sql = "SELECT * FROM THELOAI";
                tl = ds.GetTable(sql);
            }
            catch (Exception)
            {
                throw;
            }
            return tl;

        }
        public int InsertTheLoai(TheLoai tl)
        {
            int kq = 0;
            try
            {
                String sql = String.Format("INSERT INTO THELOAI(TL_TENTHELOAI) VALUES (N'{0}')",tl.TenTheLoai);
                kq = ds.ExcuteInsert(sql);
                ds.Close();
            }
            catch (Exception)
            {
                throw;
            }
            return kq;

        }
        public int DeleteTheLoai(string maTheLoai)
        {
            int kq = 0;
            try
            {
                String sql = "DELETE FROM THELOAI WHERE TL_MATHELOAI=" + maTheLoai;
                kq = int.Parse(ds.ExcuteInsert(sql).ToString());
            }
            catch (Exception)
            {
                throw;
            }
            return kq;

        }
        public int UpdateTheLoai(TheLoai tl)
        {
            int kq = 0;
            try
            {
                String sql = "UPDATE THELOAI SET TL_TENTHELOAI=N'" + tl.TenTheLoai + "' WHERE TL_MATHELOAI=" + tl.MaTheLoai;
                kq = int.Parse(ds.ExcuteInsert(sql).ToString());
            }
            catch (Exception)
            {
                throw;
            }
            return kq;

        }
    }
}
